package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override

    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        //1.查询菜品信息
        //1.1 设置查询条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNoneEmpty(name), Dish::getName, name);//where name like %#{name}%

        //1.2设置分页条件
        Page<Dish> page = new Page<>(pageNum, pageSize);

        //1.3查询
        page = dishMapper.selectPage(page, wrapper);
        System.out.println(page);

        //2.获取菜品集合
        List<Dish> dishList = page.getRecords();
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //3. 根据菜品的分类id从分类表中查询分类信息
                Category category = categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());

                //4.根据菜品的id从口味表中查询口味列表
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavorList);
            }
        }
        return page;
    }

    @Transactional //在业务层代码中,如果一个方法对多张表执行增删改方法,需要控制事务
    @Override
    public void save(Dish dish) {
        //1.将菜品基本信息保存到菜品表中
        log.info("保存菜品之前的dish对象:" + dish);
        dishMapper.insert(dish);
        log.info("保存菜品之后的dish对象:" + dish);

        //2.遍历口味列表,依次设置dish_id, 保存到口味表中
        List<DishFlavor> dishFlavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(dishFlavorList)) {
            for (DishFlavor dishFlavor : dishFlavorList) {
                //3.设置口味对象对应的菜品的id
                dishFlavor.setDishId(dish.getId());
                //4.保存口味
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public Dish findById(Long id) {
        //1.根据菜品id从菜品表获取菜品信息
        Dish dish = dishMapper.selectById(id);

        //2.根据菜品id从口味表获取口味列表
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper);
        dish.setFlavors(dishFlavorList);
        return dish;
    }

    @Transactional
    @Override
    public void update(Dish dish) {
        //1. 根据id更新菜品基本信息
        dishMapper.updateById(dish);

        //2. 根据菜品id删除口味表中原有的口味
        //delete from dish_flavor where dish_id = ""
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(wrapper);

        //3. 重新保存口味
        List<DishFlavor> dishFlavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(dishFlavorList)) {
            for (DishFlavor dishFlavor : dishFlavorList) {
                //3-1. 设置口味对象对应的菜品的id
                dishFlavor.setDishId(dish.getId());
                //3-2. 保存口味
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Transactional
    @Override
    public void deleteByIds(long[] ids) {
        //判断要删除的商品是否停售
        //select count(0) from dish where id in #(ids) and status = 1
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId, ids);
        wrapper.eq(Dish::getStatus, 1);
        Integer count = dishMapper.selectCount(wrapper);
        if (count > 0) {
            throw new CustomException("要删除的商品还在销售状态，无法删除");
        }

        //遍历删除
        for (long id : ids) {
            //2.1根据菜品id删除菜品的口味列表
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId, id);
            dishFlavorMapper.delete(wrapper1);
            //3.根据菜品id删除菜品信息
            dishMapper.deleteById(id);
        }
    }

    @Transactional
    @Override
    public void updateByIds(Long[] ids, Integer status) {
        for (Long id : ids) {
            //update dish set status = #{} where id = #{}
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishMapper.updateById(dish);
        }
    }

    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        //1. 组装查询条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //select * from dish where category_id = #{} and status = 1
        wrapper.eq(Dish::getCategoryId, categoryId).eq(Dish::getStatus, 1);

        //2.执行查询,得到菜品列表
        List<Dish> dishList = dishMapper.selectList(wrapper);

        //3.遍历菜品列表
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //从口味表根据菜品id查询
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavorList);
            }
        }
        return dishList;
    }

    @Override
    public List<Dish> findByName(String name) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNoneEmpty(name), Dish::getName, name);//where name like %#{name}%
        return dishMapper.selectList(wrapper);
    }

}
